//
//  RoothomeViewController.m
//  F50
//
//  Created by 郝 巍 on 15/5/7.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "RoothomeViewController.h"
#import "RoothomeTableViewCell.h"
#import "LoginViewController.h"
#import "RoothomeDataModle.h"
#import "ProjectInfoViewController.h"


@interface RoothomeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *dataArr;
    UIImageView *isCare;
}

@end

@implementation RoothomeViewController

- (void)createBackground
{
    UIView *nav_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    nav_bg.backgroundColor = RGB(59, 107, 178);
    [self.view addSubview:nav_bg];
 
    
    UILabel *nav_title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 25, 160, 30)];
    nav_title.text = @"项目大厅";
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font = [UIFont boldSystemFontOfSize:20];
    nav_title.textColor = [UIColor whiteColor];
    nav_title.backgroundColor = [UIColor clearColor];
    [nav_bg addSubview:nav_title];
    
    
    
}

-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;

    dataArr = [[NSMutableArray alloc]init];
    
    
    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",nil];
    NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"search",@"class",@"search",@"func",date,@"param", nil];
    
    [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
        
        NSArray *array = [result objectForKey:@"result"];
        
        for (int i = 0; i < array.count; i++) {
            NSDictionary *dict=array[i];
            RoothomeDataModle *modle=[[RoothomeDataModle alloc] init];
            modle.title = [dict objectForKey:@"title"];
            modle.imageUrl = [dict objectForKey:@"thumb"];
            modle.projectID = [dict objectForKey:@"id"];
            modle.releasetime = [dict objectForKey:@"releasetime"];
            modle.info = [dict objectForKey:@"description"];
            modle.stage = [dict objectForKey:@"stage"];
            modle.state = [dict objectForKey:@"state"];
            modle.relations = [dict objectForKey:@"relations"];
            modle.deals = [dict objectForKey:@"deals"];
            modle.amount = [dict objectForKey:@"amount"];
            modle.isrelation = [dict objectForKey:@"isrelation"];
            modle.industry = [dict objectForKey:@"industry"];
            
            [dataArr addObject:modle];
        }
        [_tableView reloadData];

    } fail:^{
        
    }];

    
  
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self createBackground];
    // 判断用户是否登录过
   
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 -49) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    _tableView.scrollEnabled = NO; //设置tableview 不能滚动
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
    
    isCare = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 90, 105)];
    isCare.center = CGPointMake(WIDTH/2, HEIGHT/2);
//    isCare.image = [UIImage imageWithImageName:@"care@2x"];
    isCare.alpha = 0;
    [self.view addSubview:isCare];
    
    if ([[USER_DEFAULT valueForKey:@"phone"] length] <= 0)
    {
        LoginViewController * login = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:login animated:YES];
        return;
    }
   
    
    
}
#pragma mark ----- uitableviewdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier = @"cell";
    RoothomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[RoothomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.nav = self.navigationController;
    RoothomeDataModle *modle = dataArr[indexPath.row];
    [cell setCellLableText:modle];
    cell.blocks = ^(int isrelation){
        if (isrelation) {
            isCare.image = [UIImage imageWithImageName:@"care@2x"];

            isCare.alpha = 1;

//            [UIView beginAnimations:nil context:nil];
//            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//            [UIView setAnimationDuration:1.5f];
//            isCare.alpha = 0;
//
//            [UIView commitAnimations];
            
            
            [UIView animateWithDuration:1.0
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseInOut //设置动画类型
                             animations:^{
                                 //开始动画
                                isCare.alpha = 0;
                             }
                             completion:^(BOOL finished){
                                 // 动画结束时的处理  
                             }];

          
        }else{
            NSLog(@"weiguanzhu");
            
            isCare.image = [UIImage imageWithImageName:@"delete@2x"];
            isCare.alpha = 1;
            
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDuration:1.0f];
            isCare.alpha = 0;
            
            [UIView commitAnimations];
            
            
            
        }
        
    };
//    [cell.favourite addTarget:self action:@selector(doFavourite:) forControlEvents:UIControlEventTouchUpInside];
    [cell.chat addTarget:self action:@selector(touchChat) forControlEvents:UIControlEventTouchUpInside];
    

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ( [[USER_DEFAULT objectForKey:@"iscertificated"] intValue] == 1) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有进行认证" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
     
        return;
    }
    
    ProjectInfoViewController *pInfo = [[ProjectInfoViewController alloc]init];
    pInfo.modle = dataArr[indexPath.row];
    [self.navigationController pushViewController:pInfo animated:YES];
    
    

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


-(void)doFavourite:sender{
    
}

-(void)touchChat{
    self.tabBarController.selectedIndex = 2;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
